//
//  LoginNet.m
//  SportSocial
//
//  Created by yull on 14/12/4.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "UserNet.h"

@implementation UserNet


+(AFHTTPRequestOperation *)confirmCodeWithPhone:(NSString *)phoneNumber
               successBlock:(httpSuccessBlock)successBlock
                  failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/register/verifyCode",[self baseUrl]];
    NSDictionary *param = @{@"mobile":phoneNumber};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    AFHTTPRequestOperation *operation = [manager POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    
    return operation;
}

+ (AFHTTPRequestOperation *)registerWithMobile:(NSString *)mobile
                                          name:(NSString *)name
                                           sex:(NSInteger)sex
                                      password:(NSString *)password
                                    verifyCode:(NSString *)verifyCode
                                  successBlock:(httpSuccessBlock)successBlock
                                     failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/register",[self baseUrl]];
    NSDictionary *param = @{@"mobile":mobile,
                            @"name":name,
                            @"sex":[NSNumber numberWithInt:sex],
                            @"password":password,
                            @"verify_code":verifyCode};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    AFHTTPRequestOperation *operation = [manager POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)logOutWithToken:(NSString *)token
                               successBlock:(httpSuccessBlock)successBlock
                                  failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/logout",[self baseUrl]];
    
    NSDictionary *param = @{@"token":token};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    AFHTTPRequestOperation *operation = [manager POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)updateUserWithAddress:(NSString *)address
                                              sex:(NSInteger)sex
                                        signature:(NSString *)signature
                                      friendCheck:(BOOL)friendCheck
                                           height:(CGFloat)height
                                           weight:(CGFloat)weight
                                     successBlock:(httpSuccessBlock)successBlock
                                        failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/update",[self baseUrl]];
    
    NSDictionary *param = @{@"token":[self token],
                            @"sex":[NSNumber numberWithInt:sex],
                            @"signature":signature,
                            @"friend_check":[NSNumber numberWithBool:friendCheck],
                            @"height":@(height),
                            @"weight":@(weight)};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;

}

+ (AFHTTPRequestOperation *)uploadIconWithIconData:(NSData *)iconData
                                   successBlock:(httpSuccessBlock)successBlock
                                      failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/uploadIcon?token=%@",[self baseUrl], [self token]];
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:iconData name:@"photo" fileName:@"photo.jpg" mimeType:@"multipart/form-data"];
    } success:successBlock failure:failBlock];
    
    return operation;
}

+ (AFHTTPRequestOperation *)searchUserWithName:(NSString *)name
                                    pageNumber:(NSInteger)page
                                      pageSize:(NSInteger)size
                                  successBlock:(httpSuccessBlock)successBlock
                                     failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/search",[self baseUrl]];
    NSDictionary *param = @{@"token":[self token],
                            @"name":name,
                            @"page_number":[NSNumber numberWithInteger:page],
                            @"page_size":[NSNumber numberWithInteger:size]};
    
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)queryUserWithUserId:(uint64_t)userId
                                   successBlock:(httpSuccessBlock)successBlock
                                      failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/query",[self baseUrl]];
    
    NSDictionary *param = @{@"token":[self token],
                            @"id":[NSNumber numberWithLongLong:userId]};
    
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)locateWithToken:(NSString *)token
                                   latitude:(CGFloat)latitude
                                  longitude:(CGFloat)longitude
                               successBlock:(httpSuccessBlock)successBlock
                                  failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/locate",[self baseUrl]];
    
    NSDictionary *param = @{@"token":token,
                            @"latitude":[NSNumber numberWithFloat:latitude],
                            @"longitude":[NSNumber numberWithFloat:longitude]};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    AFHTTPRequestOperation *operation = [manager POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)updateIOSdeviceTokenSuccessBlock:(httpSuccessBlock)successBlock
                                                   failBlock:(httpFailBlock)failBlock;
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/updateIosDeviceToken", [self baseUrl]];
    
    NSDictionary *params = @{@"token":[self token],
                             @"ios_device_token":[[CurrentUser shareInstance] deviceToken]};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    AFHTTPRequestOperation *operation = [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

@end
